import { useEffect, useState } from 'react'
import { Table, Tag, Space, Button, Card } from 'antd'
import { EditOutlined, DeleteOutlined } from '@ant-design/icons'
import request from '@/utils/request'
import img404 from '@/assets/error.png'
import { history } from '@/utils/history'

export default function List({ filterParmas }) {
  // 表头内容
  const columns = [
    {
      title: '封面',
      dataIndex: 'cover',
      width: 120,
      render: cover => {
        return <img src={cover || img404} width={80} height={60} alt="" />
      }
    },
    {
      title: '标题',
      dataIndex: 'title',
      width: 220
    },
    {
      title: '状态',
      dataIndex: 'status',
      render: data => <Tag color="green">审核通过</Tag>
    },
    {
      title: '发布时间',
      dataIndex: 'pubdate'
    },
    {
      title: '阅读数',
      dataIndex: 'read_count'
    },
    {
      title: '评论数',
      dataIndex: 'comment_count'
    },
    {
      title: '点赞数',
      dataIndex: 'like_count'
    },
    {
      title: '操作',
      render: data => {
        return (
          <Space size="middle">
            <Button type="primary" shape="circle" onClick={() => history.push(`/publish?id=${data.id}`)} icon={<EditOutlined />} />
            <Button
              type="primary"
              danger
              shape="circle"
              onClick={() => delItem(data)}
              icon={<DeleteOutlined />}
            />
          </Space>
        )
      }
    }
  ]
  // 表格体内容

  // 定义渲染列表数组
  const [renderList, setRenderList] = useState({
    count: 0,
    results: []
  })
  // 定义请求参数
  const [parmas, setParams] = useState({
    page: 1, // 当前页码
    per_page: 10 // 每页条数
  })

  useEffect(() => {
    // 获取数据
    const loadList = async () => {
      const res = await request({
        url: '/mp/articles',
        parmas
      })
      setRenderList({
        count: res.data.total_count,
        results: res.data.results
      })
    }
    loadList()
  }, [parmas])

  // 过滤参数
  useEffect(() => {
    if (!Object.keys(filterParmas).length) return
    const { status, channel_id, date } = filterParmas
    // 存储新规则
    const _parmas = {}
    _parmas.status = status
    if (channel_id) {
      _parmas.channel_id = channel_id
    }
    if (date) {
      // 格式化数据
      _parmas.begin_pubdate = date[0].format('YYYY-MM-DD')
      _parmas.end_pubdate = date[1].format('YYYY-MM-DD')
    }
    // 合并参数
    setParams({ ...parmas, ..._parmas })
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [filterParmas])

  // 删除
  const delItem = async data => {
    await request({
      url: `/mp/articles/${data.id}`,
      method: 'DELETE',
    })
    // 更新渲染
    setParams({
      page: 1,
      per_page: 10
    })
  }
  return (
    <div>
      <Card title={`根据筛选条件共查询到 ${renderList.results.length} 条结果：`}>
        <Table rowKey="id"
          columns={columns}
          dataSource={renderList.results}
          pagination={{
            pageSize: 5,
            total: renderList.results.length
          }}
        />
      </Card>
    </div>
  )
}